#include "Bench.h"
#include "World.h"
#include "WorldInfoController.h"
#include "SharedConstants.h"
#include "GroundTypes.h"
#include "HeightMapCoordinates.h"

Bench::Bench(World &i_world, WorldInfoController& i_control)
  : m_world(i_world),
  m_control(i_control)
  {
  m_type = "DEFAULT_BENCH_0";
  m_name = m_type + " " + std::to_string(m_id_cnt++);
  m_angle = 0.0;

  m_world.AddRenderable(this);
  }

double Bench::GetAngleAroundY() const 
  {
  return m_angle;
  }

void Bench::SetPosition(double i_coord_x, double i_coord_y, double i_coord_z)
  {
  SetPosition(TVector3(i_coord_x, i_coord_y, i_coord_z));
  }

void Bench::SetPosition(const TVector3& i_position)
  {
  m_position = i_position;
  _RecalculateAngle();
  }

void Bench::_RecalculateAngle()
  {
  /* Considering initial facing direction to be (0, 1) */
  TVector2 pos(GetPosition().X(), GetPosition().Y());

  EWorldEntityTypes cell_type;
  if (m_control.GetEntityOnCell(cell_type, HeightMapUtils::FindHeightMapIndexes(pos + TVector2(1,  0) * GroundConstants::CELL_SIZE)) && cell_type == WE_WALKWAY)
    m_angle = 90.0;
  else if (m_control.GetEntityOnCell(cell_type, HeightMapUtils::FindHeightMapIndexes(pos + TVector2(0, -1) * GroundConstants::CELL_SIZE)) && cell_type == WE_WALKWAY)
    m_angle = 180.0;
  else if (m_control.GetEntityOnCell(cell_type, HeightMapUtils::FindHeightMapIndexes(pos + TVector2(-1, 0) * GroundConstants::CELL_SIZE)) && cell_type == WE_WALKWAY)
    m_angle = 270.0;
  else
    m_angle = 0.0;
  }

Bench::~Bench()
  {
  m_world.RemoveRenderable(this);
  }

